﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WebDataGet.Utils
{
    public class MyXmlConfigAutoHelper<T>
    {
        public T Setting { get; set; }
        public string FileName { get; set; } = "MyConfig.xml";

        public string BackupName
        {
            get
            {
                var regex = new Regex(@"(\w+)\.(\w+)$");
                var filename = regex.Match(FileName).Value;
                var backName = (new Regex(@"\.(\w+)$")).Split(filename)[0];
                var newBackName = backName + "_back";

                return (new Regex(backName)).Replace(FileName, newBackName);
            }
        }

        /// <summary>
        /// 备份
        /// </summary>
        private MyXmlConfigHelper<T> backupXml { get; set; }

        private MyXmlConfigHelper<T> settingXml { get; set; }

        public MyXmlConfigAutoHelper()
        {

        }

        public MyXmlConfigAutoHelper(string fileName)
        {
            FileName = fileName;
        }
        public MyXmlConfigAutoHelper(string fileName, T setting)
        {
            Setting = setting;
            FileName = fileName;
        }

        /// <summary>
        /// 实例化
        /// </summary>
        public void AutoInit()
        {
            settingXml = new MyXmlConfigHelper<T>(Setting, FileName);
            backupXml = new MyXmlConfigHelper<T>(Setting, BackupName);
            //如果备份也损坏了，就GG了
            var settingFlag = true;
            var backupFlag = true;
            try
            {
                settingXml.Init();
            }
            catch (Exception ex)
            {
                Console.WriteLine("主文件读取失败");
                Console.WriteLine(ex.Message);
                settingFlag = false;
            }

            try
            {
                backupXml.Init();
            }
            catch (Exception ex)
            {
                Console.WriteLine("备份文件读取失败");
                Console.WriteLine(ex.Message);
                backupFlag = false;
            }
            Console.WriteLine($"文件完整性:setting[{settingFlag},backup[{backupFlag}]]");

            if (!backupFlag && !settingFlag)
            {
                Console.WriteLine("主要和备份文件完全破损，默认值覆盖");
                backupXml.ReInit();
                settingXml.ReInit();
            }
            else if (!backupFlag)
            {
                Console.WriteLine("备份文件完全破损，主要文件覆盖");
                backupXml.Setting = settingXml.Setting;
                backupXml.ReInit();
            }
            else if (!settingFlag)
            {
                Console.WriteLine("主要文件完全破损，备份文件覆盖");
                settingXml.Setting = backupXml.Setting;
                settingXml.ReInit();
            }
            else
            {
                Console.WriteLine("主要和备份文件正常，主要文件覆盖备份文件");
                backupXml.Setting = settingXml.Setting;
                backupXml.ReInit();
            }
            Setting = settingXml.Setting;
        }

        public void ReInit()
        {
            settingXml = new MyXmlConfigHelper<T>(Setting, FileName);
            backupXml = new MyXmlConfigHelper<T>(Setting, BackupName);
            settingXml.ReInit();
            backupXml.ReInit();
        }
    }

}
